<?php
namespace Home\Controller;

use Home\Common\Common;

class MenuController extends Common{
    //菜单列表
    public function index(){
        $menu = M('menu');
        if (empty($_GET['name'])){
            $where = array('hidden'=>1);
        }else{
            $where = array('`name` like "%'.$_GET['name'].'%"','hidden'=>1);
            $this->assign('name',$_GET['name']);
        }
        $count = $menu->where($where)->count();
        $page = new \Think\Page($count,6);
        $show = $page->show();
        $m_list = $menu->where($where)->limit($page->firstRow.','.$page->listRows)->order('mid')->select();
        $this->assign('m_list',$m_list);
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->display();
    }
    //添加菜单信息
    public function add(){
        //判断是否有提交
        if(empty($_POST['submit'])){
            $this->display();
        }else if(empty($_POST['name'])){
            $this->error('请填写权限名称',U('add'));
        }else{
            $menu = M('menu');
            if(!empty($_POST['name']))
                $menu->add($_POST);
            $this->success('添加成功',U('index'));
        }
    }
    //隐藏菜单信息
    public function del(){
        if (empty($_GET['mid']) && empty($_GET['hidden'])){
            $this->error('参数错误',U('index'));
        }else{
            $menu = M('menu');
            $_GET['hidden']=0;//修改状态为0（隐藏）
            $menu->save($_GET);
            $this->success('隐藏成功',U('index'));
        }
    }
    //批量隐藏
    public function del_all(){
        $menu = M('menu');
        if(empty($_GET['mids'])){
            $this->error('请选择需要隐藏的内容',U('index'));
        }
        $mid = implode(',',$_GET['mids']);//获取id
        $str = explode(",",$mid);//去逗号
        foreach ($str as $a){//循环id
            $menu->where(array('mid'=>$a))->save(array('hidden'=>0));
        }
        $this->success('隐藏成功',U('index'));
    }
    //查看隐藏的菜单信息
    public function menu_view(){
        $menu = M('menu');
        if (empty($_POST['name'])){
            $where = array('hidden'=>0);
        }else{
            $where = array('`name` like "%'.$_POST['name'].'%"','hidden'=>0);
            $this->assign('name',$_POST['name']);
        }

        $count = $menu->where($where)->count();
        $page = new \Think\Page($count,6);
        $show = $page->show();
        $menu_l = $menu->where($where)->limit($page->firstRow.','.$page->listRows)->order('mid')->select();
        $this->assign('count',$count);
        $this->assign('show',$show);
        $this->assign('menu_l',$menu_l);
        $this->display();
    }
    //显示岗位信息
    public function xs(){
        if (empty($_GET['mid']) && empty($_GET['hidden'])){
            $this->error('参数错误',U('menu_view'));
        }else{
            $menu = M('menu');
            $_GET['hidden']=1;//修改状态为1（显示）
            $menu->save($_GET);
            $this->success('显示成功',U('index'));
        }
    }
    //批量显示
    public function xs_all(){
        $menu = M('menu');
        if(empty($_GET['mids'])){
            $this->error('请选择需要显示的内容',U('menu_view'));
        }
        $mids = implode(',',$_GET['mids']);//获取id
        $str = explode(",",$mids);//去逗号
        foreach ($str as $a){//循环id
            $menu->where(array('mid'=>$a))->save(array('hidden'=>1));
        }
        $this->success('显示成功',U('index'));
    }
    //修改菜单信息
    public function edit(){
        if(empty($_POST['submit'])){
            if(empty($_GET['mid'])){
                $this->error('参数错误',U('index'));
            }
            $menu = M('menu');
            $menu = $menu->where('`mid`='.$_GET['mid'])->find();
            $this->assign('menu',$menu);
            $this->display();
        }else{
            $menu = M('menu');
            if(empty($_POST['name'])){
                $this->error('请将信息填写完整',U('edit'));
            }
            if (!$menu->create('',2)){
                $this->error($menu->getError());
            }
            $menu->where('`mid`='.$_POST['mid'])->save();
            $this->success('修改成功',U('index'));
        }
    }
    //删除菜单
    public function delete(){
        if (empty($_GET['mid']) && empty($_GET['hidden'])){
            $this->error('参数错误',U('index'));
        }else{
            $menu = M('menu');
            $menu->where('mid='.$_GET['mid'])->delete($_GET);
            $this->success('删除成功',U('index'));
        }
    }
}